package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Backdown;
import com.neudu.yiyang.pojo.Customer;
import com.neudu.yiyang.service.BackdownService;
import com.neudu.yiyang.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BackdownController {
    @Autowired
    private BackdownService backdownService;
    @Autowired
    private JSONReturn jsonReturn;
    @Autowired
    private CustomerService customerService;

    //查询所有退住信息
    @RequestMapping("get_all_backdowns")
    public String getAll(@RequestBody PageInfo pageInfo) {
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            //可以根据数据库设置一个state字段值，匹配在住的客户查询
            List<Backdown> list = backdownService.list();
            PageInfo<Backdown> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_backdown_by")
    public String getByCondition(@RequestBody Backdown backdown) {
        try {
            LambdaQueryWrapper<Backdown> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(!ObjectUtils.isEmpty(backdown.getId()), Backdown::getId, backdown.getId())
                    .eq(!ObjectUtils.isEmpty(backdown.getRemarks()), Backdown::getRemarks, backdown.getRemarks())
                    .like(!ObjectUtils.isEmpty(backdown.getCustomerId()), Backdown::getCustomerId, backdown.getCustomerId())
                    .eq(!ObjectUtils.isEmpty(backdown.getRetreatreason()), Backdown::getRetreatreason, backdown.getRetreatreason())
                    .eq(!ObjectUtils.isEmpty(backdown.getAuditperson()), Backdown::getAuditperson, backdown.getAuditperson());
            List<Backdown> backdowns = backdownService.list(queryWrapper);
            return jsonReturn.returnSuccess(backdowns);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("add_one_backdown")
    public String addOne(@RequestBody Backdown backdown) {
        try {
            backdownService.save(backdown);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("getbackdownby_id")
    public String getById(@RequestParam("id") Integer id) {
        try {
            Backdown backdown = backdownService.getById(id);
            return jsonReturn.returnSuccess(backdown);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modifybackdownby_id")
    public String modifyById(@RequestBody Backdown backdown) {
        try {
            backdownService.updateById(backdown);
            return jsonReturn.returnSuccess(backdown);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("removebackdown_by_id")
    public String removeByid(@RequestParam("id") Integer id) {
        try {
            backdownService.removeById(id);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 新增更新客户状态的接口
    @RequestMapping("update_customer_state")
    public String updateCustomerState(@RequestBody Customer customer) {
        Integer id = customer.getId();
        Integer state = customer.getState();
        if (id == null || state == null) {
            return jsonReturn.returnError("传入的客户ID或状态不能为空");
        }
        try {
            Customer existingCustomer = customerService.getById(id);
            if (existingCustomer!= null) {
                existingCustomer.setState(state);
                customerService.updateById(existingCustomer);
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("未找到对应的客户");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}